﻿using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xarmarin.GoldFinger.Common.Utils;
using Xarmarin.GoldFinger.Data;
using Xarmarin.GoldFinger.Engine.Core;
using Xarmarin.GoldFinger.Models.AutoModels;

namespace Xarmarin.GoldFinger.Services
{    
    /// <summary>
    /// 功能描述    ：SubjectStore  
    /// 创 建 者    ：KnifeZ
    /// 创建日期    ：2019/1/27 10:27:29 
    /// 最后修改者  ：KnifeZ
    /// 最后修改日期：2019/1/27 10:27:29 
    /// </summary>
   public class SubjectDataStore
    {
        public static bool CheckIsEmpty()
        {
            using (var conn = new SQLiteConnection(CoreEngine.ConnectionString))
            {
                DbContext db = new DbContext();
                if (!db.ExistsTable<Auto_Subject>(nameof(Auto_Subject)))
                {
                    return false;
                }
                var result = conn.Table<Auto_Subject>();
                if (result.Any())
                {
                    return false;
                }
                return true;
            }
        }
        /// <summary>
        /// 绑定当前角色
        /// </summary>
        /// <param name="CurrUser"></param>
        public static void BindCurrentSub(Auto_Subject CurrSub=null)
        {
            using (var conn = new SQLiteConnection(CoreEngine.ConnectionString))
            {
                if (CurrSub != null)
                {
                    KzContext.CurrSubject = CurrSub;
                    KzContext.CurrSub = CurrSub.RealName;
                    CurrSub.UpdatedTime = DateTime.Now;
                    conn.Update(CurrSub);
                }
                else
                {
                    var result = conn.Table<Auto_Subject>().OrderByDescending(o => o.UpdatedTime).FirstOrDefault();
                    if (result != null)
                    {
                        KzContext.CurrSubject = result;
                        KzContext.CurrSub = result.RealName;
                    }
                    else
                    {
                        AddRandSubject(new Auto_Subject()
                        {
                            RealName = Engine.Rand.RandName.Name,
                            Account = "A" + Guid.NewGuid().ToString(),
                            IcoFace = "f_jsmile.png",
                            SectName = "无门散修",
                            SectPosition = (long)SectPosition.UnKnow,
                            SectPositionStr = SectPosition.UnKnow.GetDescription(),
                            State = (long)SimpleState.Default,
                            StateStr = SimpleState.Default.GetDescription(),
                            SmallState = (long)SmallState.Inferior,
                            SmallStateStr = SmallState.Inferior.GetDescription(),
                        });
                    }
                }
            }
        }
        public static bool AddRandSubject(Auto_Subject model)
        {

            using (var conn = new SQLiteConnection(CoreEngine.ConnectionString))
            {
                model.ID = conn.Table<Auto_Subject>().Count() + 1;
                model.Code = "U" + KnifeHelper.RandomCode;
                model.Status = 1;
                model.CreatedTime = DateTime.Now;
                model.UpdatedTime = DateTime.Now;
                var result = conn.Insert(model);
                if (result > 0)
                {
                    KzContext.CurrSubject = model;
                    KzContext.CurrSub = model.RealName;
                    return true;
                }
                return false;
            }
        }
        public static bool UpdateModel(Auto_Subject model)
        {
            using (var conn = new SQLiteConnection(CoreEngine.ConnectionString))
            {
                int r=conn.Update(model);
                return r > 0 ? true : false;
            }
        }
    }
}
